import requests
import pytest

from api.reg_login_api import RegLoginApi
from common.utils import common_assert, clear_user_info, read_json


class TestRegLogin(object):
    def setup_class(self):
        clear_user_info()
    def setup(self):
        self.ses = requests.session()
        self.reg_login_api = RegLoginApi(self.ses)
    def teardown(self):
        self.ses.close()

    # 获取图片验证码测试用例
    @pytest.mark.parametrize("r,exp_status_code",read_json("reg_login_data","img_verify_code"))
    def test01_img_verify_code(self,r,exp_status_code):
        # ses =requests.session()
        # reg_login_api =RegLoginApi(ses)
        resp =self.reg_login_api.get_img_verify_code(r)
        print("获取图片验证码返回的状态码",resp.status_code)
        assert exp_status_code==resp.status_code
        # ses.close()

    # 获取短信验证码测试用例
    @pytest.mark.parametrize("phone,imgVerifyCode,exp_status_code,exp_status,description",read_json("reg_login_data","phone_verify_code"))
    def test02_phone_verify_code(self,phone,imgVerifyCode,exp_status_code,exp_status,description):
        # ses = requests.session()
        # reg_login_api = RegLoginApi(ses)
        self.reg_login_api.get_img_verify_code("12345678")
        resp =self.reg_login_api.get_phone_verify_code(phone,imgVerifyCode)
        # assert 200==resp.status_code
        # assert 200==resp.json().get("status")
        # assert "成功"in resp.json().get("description")
        common_assert(resp, exp_status_code, exp_status, description)
        # ses.close()

    # 注册测试用例
    @pytest.mark.parametrize("form_dict,exp_status_code,exp_status,description",read_json("reg_login_data","user_register"))
    def test03_register(self,form_dict,exp_status_code,exp_status,description):
        self.reg_login_api.get_img_verify_code("12345678")
        self.reg_login_api.get_phone_verify_code(form_dict.get("phone"), "8888")
        form_dict =form_dict
        resp =self.reg_login_api.user_register(form_dict)
        # assert 200 == resp.status_code
        # assert 200 == resp.json().get("status")
        # assert "成功" in resp.json().get("description")
        common_assert(resp, exp_status_code, exp_status, description)
    # 登录测试用例
    @pytest.mark.parametrize("mobile,password,exp_status_code,exp_status,description", read_json("reg_login_data", "user_login"))
    def test04_user_login(self,mobile,password,exp_status_code,exp_status,description):
        resp = self.reg_login_api.user_login(mobile,password)
        common_assert(resp, exp_status_code, exp_status, description)
    @pytest.mark.skip#跳过不执行这条测试用例
    def test05_login_pwd_error(self):
        resp1 = self.reg_login_api.user_login("13816547004","qqq1234")
        common_assert(resp1, 200, 100, "密码错误1次")
        resp2 = self.reg_login_api.user_login("13816547004", "qqq1234")
        common_assert(resp2, 200, 100, "密码错误2次")
        resp3 = self.reg_login_api.user_login("13816547004", "qqq1234")
        common_assert(resp3, 200, 100, "账号已被锁定")
        from time import sleep
        sleep(60)
        resp4 = self.reg_login_api.user_login("13816547004", "qqq123")
        common_assert(resp4, 200, 200, "成功")
if __name__ == '__main__':
    pytest.main(["-s","test01_reg_login.py"])